package com.pg.agent.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pg.agent.manager.model.mapper.AgentMapper;
import com.pg.agent.manager.model.dto.agent.AgentConvertor;
import com.pg.agent.manager.model.dto.agent.AgentDTO;
import com.pg.agent.manager.model.dto.agent.AgentPageReq;
import com.pg.agent.manager.model.dto.agent.AgentVO;
import com.pg.agent.manager.model.enity.Agent;
import com.pg.agent.manager.service.AgentService;
import com.pg.base.component.PageVO;
import com.pg.base.enums.DeleteEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 微应用表服务
 * 微应用表的应用服务
 *
 * @author admin
 * @date 2023-12-20 19:12:26
 */
@Service
public class AgentServiceImpl implements AgentService {


    @Resource
    private AgentMapper agentMapper;


    @Override
    public PageVO<AgentVO> page(AgentPageReq pageReq) {

        QueryWrapper<Agent> wrapper = getQueryWrapper(pageReq);
        wrapper.lambda().orderByDesc(Agent::getCreateTime);
        PageVO<AgentVO> result = agentMapper.selectPage(pageReq.getPageVO(), wrapper);
        return result;

    }

    @Override
    public Long add(AgentDTO AgentDTO) {
        Agent agentAdd = AgentConvertor.INSTANCE.dtoToDB(AgentDTO);

       // AgentAdd.setAgentCode(agentMapper.getAgentCode());

        agentAdd.setDeleteStatus(DeleteEnum.NORMAL.getValue());

        //todo 默认新增一个版本
        agentMapper.insert(agentAdd);

        return agentAdd.getAgentId();
    }

    @Override
    public Integer update(AgentDTO AgentDTO) {
        Agent agentEdit = AgentConvertor.INSTANCE.dtoToDB(AgentDTO);

        int count = agentMapper.updateById(agentEdit);

        return count;
    }



    @Override
    public AgentVO get(Long id) {
        Agent agent = agentMapper.selectById(id);

        return BeanUtil.copyProperties(agent, AgentVO.class);
    }

    @Override
    public Integer deleteById(Long AgentID) {
        //若该微应用上架到云市场后则该微应不能删除，若从未上架过云市场则可以删除
        Agent Agent = agentMapper.selectById(AgentID);
        if (null != Agent) {
            //删除自己
            agentMapper.deleteById(AgentID);
        }
        return 1;
    }

    public Boolean updateStatus(Long id, Integer status) {
        Agent AgentEdit = new Agent();
        AgentEdit.setStatus(status);
        AgentEdit.setAgentId(id);
        agentMapper.updateById(AgentEdit);
        return true;
    }

    private QueryWrapper<Agent> getQueryWrapper(AgentPageReq pageReq) {
        QueryWrapper<Agent> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Agent::getDeleteStatus, DeleteEnum.NORMAL.getValue());


/*        if (ObjectUtil.isNotEmpty(microApplicationQuery.getMicroAppName())) {
            wrapper.lambda().like(Agent::getMicroAppName, microApplicationQuery.getMicroAppName());
        }
        if (ObjectUtil.isNotEmpty(microApplicationQuery.getStartTime())) {
            wrapper.lambda().ge(Agent::getCreateDate, microApplicationQuery.getStartTime());
        }
        if (ObjectUtil.isNotEmpty(microApplicationQuery.getEndTime())) {
            wrapper.lambda().le(Agent::getCreateDate, microApplicationQuery.getEndTime());
        }
        if (ObjectUtil.isNotEmpty(microApplicationQuery.getStatus())) {
            wrapper.lambda().eq(Agent::getStatus, microApplicationQuery.getStatus());
        }
        if (ObjectUtil.isNotEmpty(microApplicationQuery.getProfession())) {
            wrapper.lambda().like(Agent::getProfession, microApplicationQuery.getProfession());
        }*/
        return wrapper;
    }

    
}
